
do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"



	use "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf", clear 
	sort jtidx

	merge m:1 jtidx using "$basedata/estimationdata/All_T_J/All_T_J_schoolvar_buff_0_0_lf"

	duplicates drop job_id, force

	keep job_id ncerdc_schlcode titleI
	sort job_id 

	tempfile insample
	save "`insample'", replace 
	
	
	*Vacancy timing
	use "$basedata/FOCAL_vacancies", clear
	sort job_id 
	merge m:1 job_id using `insample'
	keep if _merge==3
	drop _merge 

	gen oneper=0
	bysort job_id: replace oneper=1 if _n==1


	gen posting_month=month(earliest_app)

	label define month_lbl 2 "Feb"
	label define month_lbl 3 "March", add
	label define month_lbl 4 "April", add
	label define month_lbl 5 "May", add
	label define month_lbl 6 "June", add
	label define month_lbl 7 "July", add
	label define month_lbl 8 "August", add
	label define month_lbl 9 "September", add
	label values posting_month month_lbl 

	tab posting_month titleI if oneper==1 

	gen post = 1
	bysort posting_month: egen posting_month_count = sum(post)
	sum post
	gen posting_month_share = posting_month_count/r(sum)*100
	gen posting_titleI_share = titleI*100
	
	

	** jobs posted in april vs. july

	duplicates drop job_id, force 

	gen april_posting=(posting_month==4)
	gen july_posting=(posting_month==7)


	bysort ncerdc_schlcode: egen april_posting_school=max(april_posting)
	bysort ncerdc_schlcode: egen july_posting_school=max(july_posting)

	gen oneperschool=0
	bysort ncerdc_schlcode: replace oneperschool=1 if _n==1

	estpost tab april_posting_school july_posting_school if oneperschool==1
	eststo a 

	label var july_posting_school "Posts in July"
	label var april_posting_school "Posts in April"

	tabout april_posting_school july_posting_school if oneperschool==1 using  "$output/tables/timing_crosstab.tex", replace style(tex) bt format(0c)

	
	*Stock and Flow Waiting Table and histograms
	use "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf", clear 

	keep if applied==1
	gen DateApplied=floor(applied_date)
	
	cap drop first_person_app
	bys itidx: egen first_person_app = min(DateApplied)
	replace first_person_app=floor(first_person_app)
	
	cap drop last_person_app
	bys itidx: egen last_person_app = max(DateApplied)
	replace last_person_app=floor(last_person_app)
	
	replace PostingDate=floor(PostingDate)
	
	gen time_since_posting= DateApplied-PostingDate

	gen wait_time_instock=DateApplied-first_person_app
	gen applyday1=(wait_time_instock==0)
	gen wait_time_flow=DateApplied-PostingDate
	gen applyday1_flow=(wait_time_flow==0)

	cap drop choicesetday1
	gen choicesetday1 = 1
	replace choicesetday1 = 0 if (floor(CloseDate) < first_person_app)
	replace choicesetday1 = 0 if (floor(PostingDate) > first_person_app)

	/* now code the wait time from the next time a teacher applies */
	egen itidx2=group(itidx choicesetday1)
	 

	 sort itidx2  DateApplied
	 gen appnum = 1 if  itidx2!=itidx2[_n-1]
	 replace appnum = appnum[_n-1]+1 if itidx2==itidx2[_n-1]  & appnum==.
	 bys itidx2: egen totapps = max(appnum)
	 
	summ appnum
	local  maxapps = r(max)
	gen initial_log_on =  .
	forvalues n=1/`maxapps' {
		qui gen appdatetemp = DateApplied if appnum==`n'
		qui bys itidx2: egen appdatetemp2 = min(appdatetemp)
		qui replace initial_log_on = appdatetemp2 if appdatetemp2>=PostingDate & appdatetemp2<=CloseDate & appdatetemp2<=initial_log_on & totapps>=`n' 
		qui drop appdatetemp appdatetemp2
	}

	gen wait_time_flow_log_on =  (DateApplied-initial_log_on)
	summ wait_time_flow_log_on if choicesetday1==0, d
	gen applyday1_logon=(wait_time_flow_log_on==0)
	
	/* generate statistics in a matrix*/
	sum wait_time_instock if choicesetday1==1, d
	mat a=(r(N), r(mean), r(p50))
	sum applyday1 if choicesetday1==1
	mat a=(a,r(mean))
	sum wait_time_flow if choicesetday1==0, d
	mat b=(r(N), r(mean), r(p50))
	sum applyday1_flow if choicesetday1==0
	mat b=(b,r(mean))
	sum wait_time_flow_log_on if choicesetday1==0, d 
	mat c=(r(N), r(mean), r(p50))
	sum applyday1_logon if choicesetday1==0
	mat c=(c,r(mean))

	/* label the rows */

	gen temp_name1 = .
	label var temp_name1 "Stock"
	gen temp_name2 = .
	label var temp_name2 "Flow"
	gen temp_name3 = .
	label var temp_name3 "Flow"

	mat rownames a=temp_name1
	mat rownames b=temp_name2 
	mat rownames c=temp_name3 

	matrix j = a \c

	frmttable using "$output/tables/timing1", statmat(j) replace va tex fra ///
		ctitles("","Obs","Mean days","Median days","Share 0 days") ///
		sdec(0, 1, 0, 2 \ 0, 1, 0, 2 )

	* share of apps on day 1, day N, and in between
	preserve
	gen day1 = DateApplied==first_person_app
	gen dayN = DateApplied==last_person_app
	gen transfer_here = (next_school==1 & current_school==0)
	bys itidx: egen transfer = max(transfer) 
	
	collapse (count) num_apps=first_person_app (mean) day1 dayN time_since_posting (max) transfer, by(itidx DateApplied)
	bys itidx: egen tot_apps = sum(num_apps)
	gen frac_apps = num_apps/tot_apps
	
	bys itidx: egen tot_days = count(num_apps)
	gen frac_days = 1/tot_days
	
	gen april = month(DateApplied)<=4
	gen may = month(DateApplied)==5
	gen june = month(DateApplied)==6
	gen july = month(DateApplied)==7
	gen august = month(DateApplied)==8
	
	local i = 0
	foreach var in frac_days frac_apps time_since_posting {
		
		if `i'==0 {
			qui summ `var' if day1==1
			mat aa=(r(N))
			qui summ `var' if day1==0
			mat bb=(r(N))
		}

		qui summ `var' if day1==1
		mat aa=(aa,r(mean))
		qui summ `var' if day1==0
		mat bb=(bb,r(mean))
		
		local i = `i'+1
		
	}
	
	local i = 0
	foreach var in april may june july august {
		
		if `i'==0 {
			qui summ `var' if day1==1
			mat aaa=(r(N))
			qui summ `var' if dayN==1
			mat bbb=(r(N))
			qui summ `var' if day1==1 & transfer==1
			mat ccc=(r(N))
			qui summ `var' if dayN==1 & transfer==1
			mat ddd=(r(N))
		}

		qui summ `var' if day1==1
		mat aaa=(aaa,r(mean))
		qui summ `var' if dayN==1
		mat bbb=(bbb,r(mean))
		qui summ `var' if day1==1 & transfer==1
		mat ccc=(ccc,r(mean))
		qui summ `var' if dayN==1 & transfer==1
		mat ddd=(ddd,r(mean))
		
		local i = `i'+1
		
	}

	
	restore
	
	/* label the rows */
	
	cap drop temp_name1 temp_name2 temp_name3

	gen temp_name1 = .
	label var temp_name1 "First day"
	gen temp_name2 = .
	label var temp_name2 "Subsequent days"


	mat rownames aa=temp_name1
	mat rownames bb=temp_name2 

	matrix jj = aa \ bb

	frmttable using "$output/tables/timing2", statmat(jj) replace va tex fra ///
	ctitles("","Obs","Mean fraction of days","Mean fraction of applications","Mean days since posting") ///
	sdec(0, 2, 2, 2 \ 0, 2, 2, 2 )

	cap drop temp_name1 temp_name2

	gen temp_name1 = .
	label var temp_name1 "First day (all teachers)"
	gen temp_name2 = .
	label var temp_name2 "Last day (all teachers)"
	gen temp_name3 = .
	label var temp_name3 "First day (transfers)"
	gen temp_name4 = .
	label var temp_name4 "Last day (transfers)"

	mat rownames aaa=temp_name1
	mat rownames bbb=temp_name2 
	mat rownames ccc=temp_name3
	mat rownames ddd=temp_name4 
	
	matrix jjj = aaa \ bbb \ ccc \ ddd

	frmttable using "$output/tables/timing3", statmat(jjj) replace va tex fra ///
	ctitles("","Obs","April or before","May","June" "July" "August") ///
	sdec(0, 2, 2, 2, 2, 2 \ 0, 2, 2, 2, 2, 2 \ 0, 2, 2, 2, 2, 2 \ 0, 2, 2, 2, 2, 2 )	
	
	sum wait_time_flow_log_on if choicesetday1==0, d
	replace wait_time_flow_log_on = 10 if wait_time_flow_log_on>10 & choicesetday1 == 0
	hist wait_time_flow_log_on if choicesetday1==0, title("Application wait time") subtitle("In flow jobs") discrete xtitle("Wait time (days)") xline(`r(p50)')
	graph export "$output/figures/rel_flow_hist.png", as(png) replace
	graph export "$output/figures/rel_flow_hist_BW.eps", as(eps) replace
	
	sum wait_time_instock if choicesetday1==1, d
	replace wait_time_instock = 10 if wait_time_instock>10 & choicesetday1 == 1
	hist wait_time_instock if choicesetday1==1 & wait_time_instock<=30, title("Application wait time") subtitle("In stock jobs") discrete xtitle("Wait time (days)") xline(`r(p50)')
	graph export "$output/figures/stock_hist.png", as(png) replace
	graph export "$output/figures/stock_hist_BW.eps", as(eps) replace
	
		



	use "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf", clear 
	sort jtidx

	merge m:1 jtidx using "$basedata/estimationdata/All_T_J/All_T_J_schoolvar_buff_0_0_lf"
	
	duplicates drop job_id, force

	keep job_id ncerdc_schlcode titleI
	sort job_id 

	tempfile insample
	save "`insample'", replace 
	
	use "$basedata/estimationdata/All_T_J/All_T_J_teachvar_buff_0_0_lf", clear
	
	keep if mu_jt_preY_mean_school_ma !=.
	
	xtile va_pct_ma = mu_jt_preY_mean_school_ma, nq(100)
	
	keep applicant_id app_year va_pct_ma
	isid applicant_id app_year
	
	tempfile temp_va_teachers
	save `temp_va_teachers', replace
	
	*Timing Table
	
	use "$basedata/FOCAL_applications_data", clear
	sort job_id 
	merge m:1 job_id using `insample'
	keep if _merge==3
	drop _merge 
	merge m:1 applicant_id app_year using `temp_va_teachers'
	drop if _merge==2
	drop _merge 
	
	gen oneper=0
	bysort job_id: replace oneper=1 if _n==1

	gen post_month = month(earliest_app)
	gen app_month = month(applied_date)

	label define month_lbl 2 "Feb"
	label define month_lbl 3 "March", add
	label define month_lbl 4 "April", add
	label define month_lbl 5 "May", add
	label define month_lbl 6 "June", add
	label define month_lbl 7 "July", add
	label define month_lbl 8 "August", add
	label define month_lbl 9 "September", add
	label values post_month month_lbl
	label values app_month month_lbl 

	tab post_month titleI if oneper==1 
	
	gen post = 1 if oneper == 1
	bysort post_month: egen post_month_count = sum(post) if post==1
	sum post
	gen post_month_share = post_month_count/r(sum)*100
	bysort post_month: egen post_titleI_share = mean(titleI) if post==1
	
	quietly estpost tabstat post_month_count post_month_share post_titleI_share if post_month > 3 & post_month < 9, by(post_month) nototal
	eststo p
	
	gen app = 1
	bysort app_month: egen app_month_count = sum(app)
	sum app
	gen app_month_share = app_month_count/r(sum)*100
	bysort app_month: egen app_titleI_share = mean(titleI) if app==1
	
	quietly estpost tabstat app_month_count app_month_share app_titleI_share if app_month > 3 & app_month < 9, by(app_month) nototal
	eststo a
	
	gen hire = 1 if application_outcome == "Hired"
	bysort app_month: egen hire_month_count = sum(hire) if hire==1
	sum hire
	gen hire_month_share = hire_month_count/r(sum)*100
	bysort app_month: egen hire_titleI_share = mean(titleI) if hire==1
	
	quietly estpost tabstat hire_month_count hire_month_share hire_titleI_share if app_month > 3 & app_month < 9, by(app_month) nototal
	eststo h
	
	esttab p a h using "$output/tables/timing.tex", replace  /// 
	mtitles("\textbf{\emph{Posting}} \textbf{\emph{Applying}} \textbf{\emph{Hiring}}") ///
	collabels(\multicolumn{1}{c}{{"Count"}} \multicolumn{1}{l}{{"Share"}} \multicolumn{1}{l}{{"Share Title I"}}) ///
	addnotes("All years") /// 
	cells("post_month_count(fmt(0)) post_month_share(fmt(2)) post_titleI_share(fmt(2)) app_month_count(fmt(0)) app_month_share(fmt(2)) app_titleI_share(fmt(2)) hire_month_count(fmt(0)) hire_month_share(fmt(2)) hire_titleI_share(fmt(2))") label nonumber f noobs alignment(S) booktabs
	
	gen va_app0 = va_pct_ma != .
	bysort app_month: egen va_app0_month_count = sum(va_app0) if va_app0==1
	sum va_app0
	gen va_app0_month_share = va_app0_month_count/r(sum)*100
	bysort app_month: egen va_app0_titleI_share = mean(titleI) if va_app0==1
	
	quietly estpost tabstat va_app0_month_count va_app0_month_share va_app0_titleI_share if app_month > 3 & app_month < 9, by(app_month)
	eststo a0
	
	gen va_app50 = va_pct_ma != . & va_pct_ma >= 50
	bysort app_month: egen va_app50_month_count = sum(va_app50) if va_app50==1
	sum va_app50
	gen va_app50_month_share = va_app50_month_count/r(sum)*100
	bysort app_month: egen va_app50_titleI_share = mean(titleI) if va_app50==1
	
	quietly estpost tabstat va_app50_month_count va_app50_month_share va_app50_titleI_share if app_month > 3 & app_month < 9, by(app_month)
	eststo a50
	
	gen va_app90 = va_pct_ma != . & va_pct_ma >= 90
	bysort app_month: egen va_app90_month_count = sum(va_app90) if va_app90==1
	sum va_app90
	gen va_app90_month_share = va_app90_month_count/r(sum)*100
	bysort app_month: egen va_app90_titleI_share = mean(titleI) if va_app90==1
	
	quietly estpost tabstat va_app90_month_count va_app90_month_share va_app90_titleI_share if app_month > 3 & app_month < 9, by(app_month)
	eststo a90
	
	esttab a0 a50 a90 using "$output/tables/timing_va.tex", replace mtitles("\textbf{\emph{VA0}}" "\textbf{\emph{VA50}}" "\textbf{\emph{VA90}}") collabels(\multicolumn{1}{c}{{"Count"}} \multicolumn{1}{l}{{"Share"}} \multicolumn{1}{l}{{"Share Title I"}}) addnotes("All years") cells("va_app0_month_count(fmt(0)) va_app0_month_share(fmt(2)) va_app0_titleI_share(fmt(2)) va_app50_month_count(fmt(0)) va_app50_month_share(fmt(2)) va_app50_titleI_share(fmt(2)) va_app90_month_count(fmt(0)) va_app90_month_share(fmt(2)) va_app90_titleI_share(fmt(2))") label nonumber f noobs alignment(S) booktabs



